[cargo_new] Hint to use `cargo init` on existing dir
authorBehnam Esfahbod <behnam@zwnj.org>
Mon, 7 Aug 2017 16:31:32 +0000 (09:31 -0700)
committerBehnam Esfahbod <behnam@zwnj.org>
Mon, 7 Aug 2017 17:13:18 +0000 (10:13 -0700)
The `new` command always expects a non-existing path. The `init` command
always expects an existing path. Therefore, it makes sense to hint to
use `init` if the pre-condition to `new` is not satisfied.

Fixes <https://github.com/rust-lang/cargo/issues/4366>

src/cargo/ops/cargo_new.rs
tests/new.rs

index 6320498f64a65aa38f3b157c83f354bdd85e1be8..eddcd433c2546a89912cae08f59c5185fb87c2ff 100644 (file)
@@ -268,12 +268,14 @@ fn plan_new_source_file(bin: bool, project_name: String) -> SourceFileInformatio
 pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
     let path = config.cwd().join(opts.path);
     if fs::metadata(&path).is_ok() {
-        bail!("destination `{}` already exists",
-              path.display())
+        bail!("destination `{}` already exists\n\n\
+            Use `cargo init` to initialize the directory\
+            ", path.display()
+        )
     }
 
     if opts.lib && opts.bin {
-        bail!("can't specify both lib and binary outputs");
+        bail!("can't specify both lib and binary outputs")
     }
 
     let name = get_name(&path, &opts, config)?;
index fa75d2da870aa9edee3bbf9b64bff2fb354d7851..0e95caf029b65dba108729f55a01e01ed36ec56b 100644 (file)
@@ -111,7 +111,8 @@ fn existing() {
     fs::create_dir(&dst).unwrap();
     assert_that(cargo_process("new").arg("foo"),
                 execs().with_status(101)
-                       .with_stderr(format!("[ERROR] destination `{}` already exists\n",
+                       .with_stderr(format!("[ERROR] destination `{}` already exists\n\n\
+                                            Use `cargo init` to initialize the directory",
                                             dst.display())));
 }